package st.business.tepth.business.user.busnumber.workholder.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;

import java.util.ArrayList;
import java.util.List;

import st.business.tepth.R;
import st.business.tepth.base.OnItemChooseListener;
import st.business.tepth.business.bean.DiagnoseBean;
import st.business.tepth.business.bean.RepairTaskBean;

/**
 * @author zhangyu
 * @date 2019/2/13
 */
public class TroublePagerAdapter extends PagerAdapter {

    private List<RepairTaskBean.FaultPhenomenonListBean> mTroubleBeans;
    private OnItemChooseListener<DiagnoseBean> mDeleteTempListener;
    private OnItemChooseListener<DiagnoseBean> mDeleteServerListener;
    private OnItemChooseListener<DiagnoseBean> mAddListener;
    private OnItemChooseListener<DiagnoseBean> mRelevanceListener;
    private List<RecyclerView> mItems = new ArrayList<>();
    private SparseArray<TroubleDiagnoseAdapter> mAdapterSparseArray = new SparseArray<>();
    //0-未确认诊断未提交方案 1-已确认诊断未提交方案 2-已确认诊断已提交方案 3-作业已完成
    private int mStatus;
    //当前诊断删除关联的adapter
    private TroubleDiagnoseAdapter mDeleteAdapter;
    //当前诊断删除的position
    private int mDeletePosition = -1;

    public TroublePagerAdapter(Context context, int status,
                               List<RepairTaskBean.FaultPhenomenonListBean> list,
                               OnItemChooseListener<DiagnoseBean> deleteLocalListener,
                               OnItemChooseListener<DiagnoseBean> deleteServerListener,
                               OnItemChooseListener<DiagnoseBean> relevanceListener,
                               OnItemChooseListener<DiagnoseBean> addListener) {
        mTroubleBeans = list;
        mDeleteTempListener = deleteLocalListener;
        mDeleteServerListener = deleteServerListener;
        mAddListener = addListener;
        mRelevanceListener = relevanceListener;
        mStatus = status;
        setAdapter(context, list);
    }

    public List<RepairTaskBean.FaultPhenomenonListBean> getData() {
        return mTroubleBeans;
    }

    @Override
    public int getCount() {
        return mTroubleBeans == null ? 0 : mTroubleBeans.size();
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
        return view == o;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int index) {
        container.addView(mItems.get(index));
        return mItems.get(index);
    }

    private void setAdapter(Context context,
                            List<RepairTaskBean.FaultPhenomenonListBean> list) {
        for (RepairTaskBean.FaultPhenomenonListBean troubleBean : list) {
            RecyclerView recyclerView = new RecyclerView(context);
            recyclerView.setLayoutManager(
                    new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL
                            , false));
//            FlexboxLayoutManager commonLayoutManager = new FlexboxLayoutManager(context);
//            commonLayoutManager.setFlexWrap(FlexWrap.WRAP);
//            commonLayoutManager.setAlignItems(AlignItems.STRETCH);
//            recyclerView.setLayoutManager(commonLayoutManager);
            TroubleDiagnoseAdapter adapter = new TroubleDiagnoseAdapter
                    (R.layout.item_trouble_diagnose, troubleBean.getId(), mStatus,
                            troubleBean.getFaultDiagnosticTemporaryList());
            recyclerView.setAdapter(adapter);

            recyclerView.addOnItemTouchListener(new OnItemChildClickListener() {
                @Override
                public void onSimpleItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                    DiagnoseBean diagnoseBean = (DiagnoseBean) adapter.getItem(position);
                    if (view.getId() == R.id.iv_diagnose_delete) {
                        if (diagnoseBean != null && diagnoseBean.isTemp()) {
                            //本地临时的就自己删了
                            adapter.remove(position);
                            if (mDeleteTempListener != null) {
                                mDeleteTempListener.onChoose(diagnoseBean);
                            }
                        } else {
                            //服务器上面的删除
                            if (mDeleteServerListener != null) {
                                mDeleteAdapter = (TroubleDiagnoseAdapter) adapter;
                                mDeletePosition = position;
                                mDeleteServerListener.onChoose(diagnoseBean);
                            }
                        }
                    } else if (view.getId() == R.id.tv_add_program) {
                        if (diagnoseBean != null) {
                            if (mAddListener != null) {
                                mAddListener.onChoose(diagnoseBean);
                            }
                        }
                    } else if (view.getId() == R.id.tv_relevance_program) {
                        if (diagnoseBean != null) {
                            if (mRelevanceListener != null) {
                                mRelevanceListener.onChoose(diagnoseBean);
                            }
                        }
                    }
                }
            });
            mItems.add(recyclerView);
        }
    }

    public void notifyChange(int position, DiagnoseBean diagnoseBean) {
        RecyclerView recyclerView = mItems.get(position);
        if (recyclerView != null) {
            TroubleDiagnoseAdapter adapter = (TroubleDiagnoseAdapter) recyclerView.getAdapter();
            if (adapter != null) {
                adapter.addData(0, diagnoseBean);
            }
        }
    }

    public void notifyDelete() {
        if (mDeleteAdapter != null) {
            if (mDeletePosition != -1) {
                mDeleteAdapter.remove(mDeletePosition);
                mDeletePosition = -1;
                mDeleteAdapter = null;
            }
        }
    }

    public List<DiagnoseBean> getTroubleBeans() {
        if (mItems != null) {
            List<DiagnoseBean> all = new ArrayList<>();
            for (RecyclerView recyclerView : mItems) {
                TroubleDiagnoseAdapter adapter =
                        (TroubleDiagnoseAdapter) recyclerView.getAdapter();
                if (adapter != null) {
                    List<DiagnoseBean> list = adapter.getData();
                    List<DiagnoseBean> tempList = new ArrayList<>();
                    for (int i = 0, size = list.size(); i < size; i++) {
                        if (list.get(i).isTemp()) {
                            //只提交未确认的
                            all.add(list.get(i));
                            //将临时提交的标记以后好删除
                            mAdapterSparseArray.put(adapter.getPhenomenonId(), adapter);
                        } else {
                            tempList.add(list.get(i));
                        }
                    }
                    adapter.setDataWhenDeleteSucceed(tempList);
                }
            }
            return all;
        }
        return null;
    }

    public void replaceDiagnose(List<DiagnoseBean> list) {
        SparseArray<List<DiagnoseBean>> listSparseArray = new SparseArray<>();
        for (DiagnoseBean diagnoseBean : list) {
            int phenomenonId = diagnoseBean.getRepair_phenomenon_id();
            List<DiagnoseBean> diagnoseBeans =
                    listSparseArray.get(phenomenonId);
            if (diagnoseBeans == null) {
                diagnoseBeans = new ArrayList<>();
                listSparseArray.put(phenomenonId, diagnoseBeans);
            }
            diagnoseBeans.add(diagnoseBean);
        }
        if (mAdapterSparseArray != null) {
            for (int i = 0; i < listSparseArray.size(); i++) {
                int key = listSparseArray.keyAt(i);
                List<DiagnoseBean> diagnoseBeans = listSparseArray.get(key);
                TroubleDiagnoseAdapter adapter = mAdapterSparseArray.get(key);
                if (adapter != null) {
                    adapter.notifyAddData(diagnoseBeans);
                }
            }
        }
    }

    public List<DiagnoseBean> getDiagnoses(boolean isTemp) {
        List<DiagnoseBean> diagnoseBeans = new ArrayList<>();
        if (mItems != null) {
            for (RecyclerView recyclerView : mItems) {
                TroubleDiagnoseAdapter adapter =
                        (TroubleDiagnoseAdapter) recyclerView.getAdapter();
                if (adapter != null) {
                    List<DiagnoseBean> list = adapter.getData();
                    if (isTemp) {
                        for (DiagnoseBean diagnoseBean : list) {
                            if (diagnoseBean.isTemp()) {
                                diagnoseBeans.add(diagnoseBean);
                            }
                        }
                    } else {
                        diagnoseBeans.addAll(list);
                    }
                }
            }
        }
        return diagnoseBeans;
    }

    public RepairTaskBean.FaultPhenomenonListBean getTrouble(int position) {
        return mTroubleBeans.get(position);
    }
}
